import JQuery from 'jquery';
import Vue from 'vue';
let situation = {
    state: {
        fontList: [
            {
                name: '默认字体',
                value: 'inherit'
            }
        ],
        historyDate: {}
    },
    mutations: {
        setFontList(state, list) {
            let array = [];
            for (let index = 0; index < list.length; index++) {
                const element = list[index];
                array.push({
                    name: element.name,
                    value: element.name
                })
            }
            state.fontList = [...state.fontList, ...array];
        },
        setValidateHistory(state, obj) {
            let { type, value, flag = false } = obj;
            state.historyDate[type] = value;
            if (flag) {
                if (state.historyDate.hasOwnProperty(type)) {
                    delete state.historyDate[type];
                }
            }
        },
        setHistoryValue(state, obj) {
            if (state.historyDate.hasOwnProperty(obj.type)) {
                let array = obj.type.split('.');
                let target = null;
                if (array.length && array.length > 2) {
                    target = obj.element[array[0]];
                    for (let index = 1; index < array.length - 1; index++) {
                        const element = array[index];
                        target = target[element];
                    }
                } else {
                    target = target[obj.type.replace(`.${array[array.length - 1]}`, '')];
                }
                Vue.set(target, array[array.length - 1], +state.historyDate[obj.type]);
                if (obj.id) {
                    JQuery('#' + obj.id).val(state.historyDate[obj.type]);
                }
            }
        }
    },
    getters: {
        fontList: state => state.fontList
    }
}

export default situation